﻿using System;
using System.Net;
using System.Text;
using System.Security.Cryptography;


namespace Fetion.Util
{
    public class AlgorithmProxy : IAlgorithm
    {
        // Person constructor
        public AlgorithmProxy()
        {
        }

        public byte[] SHA_1(byte[] message)
        {
            using (SHA1 getSha1 = new SHA1Managed())
            {
                return getSha1.ComputeHash(message);
            }
        }
        public byte[] RSAEncrypt(byte[] message, byte[] M, byte[] E)
        {
            byte[] M2 = new byte[128];
            for (int i = 0; i < 128; ++i)
            {
                M2[i] = M[i + 1];
            }
            string strPublicKey =Convert.ToBase64String(M2);
            string strExponent = Convert.ToBase64String(E);
            string strPublicKeyXml = "<RSAKeyValue><Modulus>" + strPublicKey + "</Modulus><Exponent>" + strExponent + "</Exponent></RSAKeyValue>";

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] cipherbytes;
            rsa.FromXmlString(strPublicKeyXml);
            cipherbytes = rsa.Encrypt(message, false);
            return cipherbytes;
        }
        public  String AESEncrypt(byte[] str, byte[] aesKey)
        {
            string aesEncrypt = "";
            return aesEncrypt;
        }
        public  String AESDecrypt(byte[] str, byte[] aesKey)
        {
            byte[] byteIV = new byte[] { 0, 0x39, (byte)0x9f, 0x3d, 0x12, 0x5d, (byte)0xb5, 0x53, 0x0a, (byte)0xb5, (byte)0xe0, 0x00, (byte)0xd6, (byte)0xb0, (byte)0xf4, 0x5a };
            byte[] keyArray = aesKey;
            byte[] toEncryptArray = str;
            Aes aes = new AesManaged();
            aes.KeySize = 256;
            aes.BlockSize = 128;
            aes.Key = keyArray;
            aes.IV = byteIV;
            System.Security.Cryptography.ICryptoTransform cTransform = aes.CreateDecryptor();

            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return System.Text.Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
        }

        public bool RSASignatureVerify(byte[] signedMessage, byte[] signedOriginSrc, byte[] M, byte[] E)
        {
            try
            {
                //byte[] rgbHash = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(str);
                byte[] rgbHash = signedOriginSrc;
                string strTmp = Convert.ToBase64String(rgbHash);
                byte[] rgbSignature = signedMessage;
                string strPublicKey = Convert.ToBase64String(M);
                //string strPublicKey = Convert.ToBase64String(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(str));
                string strExponent = Convert.ToBase64String(E);
                string strPublicKeyXml = "<RSAKeyValue><Modulus>" + strPublicKey + "</Modulus><Exponent>" + strExponent + "</Exponent></RSAKeyValue>";

                RSAParameters paraPub = new RSAParameters();
                paraPub.Modulus = M;
                paraPub.Exponent = E;
                RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();
                //rsaPub.FromXmlString(strPublicKeyXml);
                rsaPub.ImportParameters(paraPub);

                SHA1Managed sh = new SHA1Managed();

                if (rsaPub.VerifyData(rgbHash, sh, rgbSignature))
                {
                    return true;
                }
                return false;

                //RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(rsaPub);
                //deformatter.SetHashAlgorithm("RSA");

                //if (deformatter.VerifySignature(hashValue, rgbSignature))
                //{
                //    return true;
                //}
                //return false;
            }
            catch
            {
                return false;
            }
        }

    }
}
